<?php
/**
 * 微信会员卡模块订阅器
 *
 * @author 橙橙同学
 * @url 
 */
defined('IN_IA') or exit('Access Denied');

class Orange_wechatcardModuleReceiver extends WeModuleReceiver {
	public function receive() {

        if ($this->message['event'] == 'user_get_card') {
            //领取会员卡
            //是否为转赠领取，1代表是，0代表否。
            if (empty($this->message['isgivebyfriend'])) {

                $coupon_record = pdo_get('wechatcard_record', array('card_id' => trim($this->message['cardid']), 'openid' => trim($this->message['fromusername']), 'status' => 1, 'code' => ''), array('id'));
                //如果该卡领取记录已经存在 那么更新
                if (!empty($coupon_record)) {
                    pdo_update('wechatcard_record', array('code' => trim($this->message['usercardcode'])), array('id' => $coupon_record['id']));
                } else {
                    //获取粉丝信息
                    $fans_info = mc_fansinfo($this->message['fromusername']);

                    //获取卡信息
                    $coupon_info = pdo_get('wechatcard_createcard', array('card_id' => $this->message['cardid']));

                    //获取领卡次数 ??
                    $pcount = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wechatcard_record') . " WHERE `openid` = :openid AND `couponid` = :couponid", array(':couponid' => $coupon_info['id'], ':openid' => trim($this->message['fromusername'])));

                    // 如果领卡次数小于最大领卡次数 并且会员卡总数>0
                    if ($pcount < $coupon_info['get_limit'] && $coupon_info['quantity'] > 0) {

                        $insert_data = array(
                            'uniacid' => $fans_info['uniacid'],
                            'card_id' => $this->message['cardid'],
                            'openid' => $this->message['fromusername'],
                            'code' => $this->message['usercardcode'],
                            'addtime' => TIMESTAMP,
                            'status' => '1',
                            'uid' => $fans_info['uid'],
                            'remark' => '用户通过投放扫码',
                            'cardId' => $coupon_info['id'],
                        );
                        //插入数据 更新剩余卡数量
                        pdo_insert('wechatcard_record', $insert_data);
                        pdo_update('coupon', array('quantity' => $coupon_info['quantity'] - 1, 'dosage' => $coupon_info['dosage'] + 1), array('uniacid' => $fans_info['uniacid'], 'id' => $coupon_info['id']));
                    }
                }
            } else {
                // 如果是转增领取 根据会员卡原来主人 获取会员卡的原信息
                $old_record = pdo_get('wechatcard_record', array('openid' => trim($this->message['friendusername']), 'card_id' => trim($this->message['cardid']), 'code' => trim($this->message['oldusercardcode'])));
                // 更新会员卡所属
                pdo_update('wechatcard_record', array('addtime' => TIMESTAMP, 'givebyfriend' => intval($this->message['isgivebyfriend']), 'openid' => trim($this->message['fromusername']), 'code' => trim($this->message['usercardcode']), 'status' => 1), array('id' => $old_record['id']));
            }
        } elseif ($this->message['event'] == 'user_del_card') {
            //用户删除卡券事件
            $card_id = trim($this->message['cardid']);
            $openid = trim($this->message['fromusername']);
            $code = trim($this->message['usercardcode']);

            pdo_update('wechatcard_record', array('status' => 4), array('card_id' => $card_id, 'openid' => $openid, 'code' => $code));
        } elseif ($this->message['event'] == 'user_consume_card') {
            //核销卡券事件
            $card_id = trim($this->message['cardid']);
            $openid = trim($this->message['fromusername']);
            $code = trim($this->message['usercardcode']);
            pdo_update('wechatcard_record', array('status' => 3), array('card_id' => $card_id, 'openid' => $openid, 'code' => $code));
        }
	}
}